<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\AdminController;

class CommentController extends AdminController
{
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'trade'); // 数据集
    }
    /*
     * 评论管理
     *
     */
    public function index(){
        $wxdgoods_comment = M('goods_comment');
        if(IS_GET){
            if(!empty(I('get.user_name'))){
                $user_name = I('get.user_name');
                $datas['user_name'] = array('like',"%$user_name%");
            }
            if(!empty(I('get.query_start_time'))){
                $query_start_time = strtotime(I('get.query_start_time'));
                $datas['add_time'] = array('egt',$query_start_time);
            }
            if(!empty(I('get.query_end_time'))){
                $query_end_time = strtotime(I('get.query_end_time'));
                $datas['add_time'] = array('elt',$query_end_time);
            }
            if(!empty(I('get.query_end_time')) && !empty(I('get.query_start_time'))){
                $datas['add_time'] = array('between',"$query_start_time,$query_end_time");
            }
            if(!empty(I('get.goods_id'))){
                $datas['goods_id'] = I('get.goods_id');
                $name = M('goods')->where(array('goods_id'=>$datas['goods_id']))->field('goods_title')->find();
                $goods['goods_id'] = $datas['goods_id'];
                $goods['goods_name'] = $name['goods_title'];
                $this->assign('goods',$goods);

            }
        }
        $count = $wxdgoods_comment->where($datas)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $wxdgoods_comment->where($datas)->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
//        p($list);die;
        $this->assign('list',$list);// 赋值数据集
        $this->assign('pages',$show);// 赋值分页输出

        $this->display();
    }

    /*
     * 商家回复
     *
     */
    public function replyComment(){
        $id = I('get.id');
        $wxdgoods_comment = M('goods_comment');
        if(IS_POST){
            $data['reply'] = I('post.reply');

            $res = $wxdgoods_comment->where("id = $id")->data($data)->save();
            if($res){
                $this->success('操作成功', U('index'),3);die;
            }else{
                $this->error('操作失败');die;
            }
        }
        //商户评价信息
        $comment = $wxdgoods_comment->where("id = $id")->find();
        $this->assign('comment',$comment);

        //订单信息
        $where['order_id'] = $comment['rec_id'];
        $order = D('Order')->relation('OrderCommon')->where($where)->find();
        $this->assign('order',$order);

        //用户信息
        $user = unserialize($order['OrderCommon']['reciver_info']);
        $user['dizhi'] = getCityName($user['province_id'],$user['city_id'],$user['area_id']);
        $this->assign('user',$user);

        //商品信息
        $data['order_id'] = $order['order_id'];
        $data['goods_id'] = $comment['goods_id'];
        $goods = M('order_goods')->where($data)->field('goods_num,goods_image,goods_pay_price')->find();
        $this->assign('goods',$goods);
        $this->assign('action', 'comment');
        $this->assign('id',$id);
        $this->display();
    }

    /*
     * 删除
     *
     */
    public function deleteComment(){
        $data['id'] = I('get.id');
        $wxdgoods_comment = M('goods_comment');
        $res = $wxdgoods_comment->where($data)->delete();
        if($res){
            $this->success('操作成功', U('index'),3);die;
        }else{
            $this->error('操作失败');die;
        }
    }

    public function is_show(){
        $p = I('post.');
        $where['id'] = $p['id'];
        $data['is_show'] = $p['is_show'];
        $re = M('goods_comment')->where($where)->save($data);
        $a['a'] = $re;
        $this->ajaxreturn($a);

    }

    public function addComment(){
        if(IS_GET){
            $goods['goods_id'] = I('goods_id');
            $goods['goods_name'] = I('goods_name');
            $this->assign('goods',$goods);
        }
        if(IS_POST){
            $p = I('post.');

            //是否填写对某件商品评价的id
            if(trim($p['goods_id']) && is_numeric(trim($p['goods_id']))){

                $g = M('goods')->where(array('goods_id'=>trim($p['goods_id'])))->field('goods_title,goods_created')->find();

                //商品id是否存在
                if($g){
                    $data['goods_id'] = trim($p['goods_id']);
                    $data['goods_name'] = $g['goods_title'];

                    //是否晒图
                    if($p['goods_images']){
                        $data['goods_img'] = serialize($p['goods_images']);
                    }

                    //订单id与用户id均设置为0
                    $data['rec_id'] = 0;
                    $data['user_id'] = 0;

                    // 随机名字
                    $file = fopen("Public/m.txt","r");
                    $user=array();
                    $i=0;
                    while(!feof($file))
                    {
                        $user[$i]= fgets($file);//fgets()函数从文件指针中读取一行
                        $i++;
                    }
                    fclose($file);
                    $user=array_filter($user);
                    $sjnum = rand(0,$i);
                    $data['user_name'] = $user[$sjnum];



                    //随机用户头像
                    $data['headimgurl'] = HOME_URL.'/Public/img/avatar/'.rand(1,100).'.jpg';

                    //添加评价时间
                    if($p['add_time']){
                        $t = strtotime($p['add_time'])+rand(0,3600);
                        if(($g['goods_created']+86400) < $t){
                            $data['add_time'] = $t;
                        }else{
                            $this->error('评价时间至少大于商品上架时间一天');die;
                        }
                    }else{
                        $data['add_time'] = time();
                    }

                    //是否填写评价
                    if(trim($p['comment'])){
                        $data['comment'] = trim($p['comment']);
                    }else{
                        $data['comment'] = "这位用户比较懒，没有留下任何信息";
                    }

                    //是否做出回复评价
                    if(trim($p['reply'])){
                        $data['reply'] = trim($p['reply']);
                    }

                    //描述 服务 发货 物流 是否存在
                    if(trim($p['miaoshu']) && is_numeric(trim($p['miaoshu']))){
                        $data['miaoshu'] = trim($p['miaoshu']);
                    }else{
                        $data['miaoshu'] = 5;
                    }

                    if(trim($p['fuwu']) && is_numeric(trim($p['fuwu']))){
                        $data['fuwu'] = trim($p['fuwu']);
                    }else{
                        $data['fuwu'] = 5;
                    }

                    if(trim($p['fahuo']) && is_numeric(trim($p['fahuo']))){
                        $data['fahuo'] = trim($p['fahuo']);
                    }else{
                        $data['fahuo'] = 5;
                    }

                    if(trim($p['wuliu']) && is_numeric(trim($p['wuliu']))){
                        $data['wuliu'] = trim($p['wuliu']);
                    }else{
                        $data['wuliu'] = 5;
                    }

                    //是否添加点赞次数
                    if(trim($p['thumbs_up']) && is_numeric(trim($p['thumbs_up']))){
                        $data['thumbs_up'] = trim($p['thumbs_up']);
                    }else{
                        $data['thumbs_up'] = 0;
                    }

                    //默认为显示状态
                    $data['is_show'] = 1;

                    $re = M('goods_comment')->add($data);

                    if($re){
                        $this->success('新增成功', U('index'));
                    }else{
                        $this->error('失败请稍后重试');
                    }

                }else{
                    $this->error('商品id查询不到此商品，请确认');
                }
            }else{
                $this->error('没有填写商品id或填写的id不是一个数字');
            }
        }
        $this->display();
    }
}